package cn.jly.bigdata.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @author lanyangji
 * @date 2019/11/24 10:47
 */
object Spark02_expr15 {

  def main(args: Array[String]): Unit = {

    // local模式

    // 创建 SparkConf 对象
    // 这边也自定义了本地的模式（分配多少个cpu）
    // app id
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordCount")

    // 创建spark上下文对象，SparkContext -> sc
    val sc = new SparkContext(sparkConf)

    val listRDD: RDD[String] = sc.makeRDD(Array("hello", "hello", "spark", "scala", "scala", "scala"))

    // 转换为K-V对的RDD
    val mapRDD: RDD[(String, Int)] = listRDD.map((_, 1))

    val resRDD: RDD[(String, Int)] = mapRDD.reduceByKey(_ + _)

    println(resRDD.collect.mkString(","))
  }
}
